package com.xbang.attendance.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 
 *	  计算时间差的工具类,能到年，到月，到天，
 *     User:huawei
 *     Time:2009-11-11
 *     by  上午10:22:58
 */
public class DateUtil {   
	/**
	 * yyyy-MM-dd
	 */
	public static final String DATE_FORMAT = "yyyy-MM-dd";

	/**
	 * "yyyy-MM-dd HH:mm:ss"
	 */
	public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**  
     * Main Method  
     * @param args  
     * @throws ParseException   
     */  
    public static void main(String[] args) throws ParseException {   
//        DateCountUtil td = new DateCountUtil();   
//        String[] data = td.countMonthDates(24, new Date());   
//        if(data != null && data.length > 0)   
//        for(int i = 0; i < data.length; i++)   
//        {   
//            System.out.println(data[i]);   
//        }   
        
       //
       // System.out.println(new Date().toLocaleString());
    	
    	//System.out.println(DateUtils.addDays(DateUtils.stringToDate("2009-1-12"), DateUtils.DATE_FORMAT, 1));
//        System.out.println(DateUtils.stringToDate("2009-1-12",DateUtils.DATE_FORMAT));
//        System.out.println("相差的年数：2009-1-12至2009-1-13 ="+getDistYear("2009-1-12", "2009-1-13"));
//        System.out.println("相差的月数：2009-1-12至2009-1-13 ="+getDistMonth("2009-1-12", "2009-1-13"));
        System.out.println("相差的天数：2009-11-12至2009-11-30 ="+getDistDates("2009-11-12", "2009-11-30"));
//        System.out.println(addDay(new Date(), 23));
//        
//        System.out.println(addMonth(new Date(), 1));
//        System.out.println(addYear(new Date(), 1000));
//        System.out.println("下个月的第一天"+getFirstDayOfNextMonth(new Date()));
//        //System.out.println(formatDate(getLastDayDate(new Date())));
//        System.out.println("上个月的最后一天"+getLastDayOfPreviousMonth(new Date()));
//        System.out.println("明天"+getNextDay(new Date()));
//        System.out.println("昨天"+getPreviousDay(new Date()));
//        System.out.println("明年"+getFirstDayOfNextYear(new Date()));
//        System.out.println("上年"+getLastDayOfPreviousYear(new Date()));
        
    	SimpleDateFormat sdf = new SimpleDateFormat("dd");
    	//return	sdf.format(parseDate(date, DATE_FORMAT));
    	System.out.println(getNextDay(new Date()));
    	System.out.println(getPreviousDay(new Date()));
    	
        System.out.println(sdf.format(new Date()));
        System.out.println(getDayOfSum());
    }      
    
    /***
     * 得到当月的天数
     * @return
     */
    public static int getDayOfSum(){
    	Calendar cal = Calendar.getInstance();
    	cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)+1);
    	cal.set(Calendar.DAY_OF_MONTH,1);
    	cal.set(Calendar.DATE,cal.get(Calendar.DATE)-1);
    	return cal.get(Calendar.DAY_OF_MONTH);
    }
    
    
    /**
     * 得到下一年第一天
     * @param date
     * @return
     * @throws ParseException 
     *
     */
    public static String getFirstDayOfNextYear(Date date) throws ParseException{
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(parseDate(addYear(date, 1), DATE_FORMAT));
    	calendar.set(Calendar.DAY_OF_YEAR, 1);
    	return formatDate(calendar.getTime());
    }
    /**
     * 得到上一年最后一天。
     * @param date
     * @return
     *
     */
    public static String getLastDayOfPreviousYear(Date date){
    	Calendar calendar = Calendar.getInstance();
    	Calendar ca=Calendar.getInstance();   
    	ca.setTime(date);
    	ca.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-1);
    	calendar.setTime(ca.getTime());
		return calendar.get(Calendar.YEAR)+"-12-31 23:59:59";
    }
    /**
     * 减年数
     * @param date
     * @param count
     * @return
     *
     */
    public static Date lowerYear(Date date,int count){
    	Calendar calendar=Calendar.getInstance();   
    	calendar.setTime(date);
    	calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-count);
    	return calendar.getTime();
    }
    /**  
     * 接受YYYY-MM-DD的日期字符串参数,返回两个日期相差的天数  
     * @param start  
     * @param end  
     * @return  
     * @throws ParseException  
     */  
    public static int getDistDates(String start,String end) throws ParseException     
    {   
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
          Date startDate = sdf.parse(start);      
          Date endDate = sdf.parse(end);   
          return getDistDates(startDate,endDate);   
    }   
    
    /**
     * 解析日期
     * @param value
     * @param format
     * @return
     * @throws ParseException
     *
     */
    public static Date parseDate(String value,String format) throws ParseException{
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
    	return simpleDateFormat.parse(value);
    }
    /**
     * 加天数
     * @param value
     * @param count
     * @return
     */
    public static String addDay(Date value,int count){
    	Calendar calendar=Calendar.getInstance();   
    	calendar.setTime(value);
    	calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+count);
    	return formatDate(calendar.getTime());
    }
    
    /**
     * 加月份
     * @param value
     * @param count
     * @return
     */
    public static String addMonth(Date value,int count){
    	Calendar calendar=Calendar.getInstance();   
    	calendar.setTime(value);
    	calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+count);
    	return formatDate(calendar.getTime());
    }
    /**
     * 加年数
     * @param value
     * @param count
     * @return
     */
    public static String addYear(Date value,int count){
    	Calendar calendar=Calendar.getInstance();   
    	calendar.setTime(value);
    	calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)+count);
    	return formatDate(calendar.getTime());
    }
    /**
     * 格式化时间
     * @param value
     * @return
     */
    public static String formatDate(Date value){
    	SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT);
    	return sdf.format(value);
    }
    /**
     * 格式化时间
     * @param value
     * @return
     */
    public static String formatDate(Date value,String format){
    	SimpleDateFormat sdf = new SimpleDateFormat(format);
    	return sdf.format(value);
    }
    /**
     * 格式化
     * @return
     * @throws ParseException 
     *
     */
    public static String formatString(String date,String format) throws ParseException{
    	SimpleDateFormat sdf = new SimpleDateFormat(format);
    	return	sdf.format(parseDate(date, format));
    }
    /**  
     * 返回两个日期相差的月数
     * @param startDate  
     * @param endDate  
     * @return  
     * @throws ParseException  
     */  
    public static int getDistMonth(String start,String end) throws ParseException{
    	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         Date startDate = sdf.parse(start);      
         Date endDate = sdf.parse(end);   
         return getDistMonth(startDate, endDate);
    }
    /**  
     * 返回两个日期相差的月数  
     * @param startDate  
     * @param endDate  
     * @return  
     * @throws ParseException  
     */  
    public static int getDistMonth(Date startDate,Date endDate){
    	int totalYear = getDistYear(startDate, endDate);
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(startDate);
    	int startMonth = calendar.get(Calendar.MONTH);
    	calendar.setTime(endDate);
    	int endMonth = calendar.get(Calendar.MONTH);
    	return endMonth - startMonth + totalYear * 12;
    }
    /**
     * 得到上个月最后一天
     * @return
     *
     */
    public static String getLastDayOfPreviousMonth(Date date){
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);
    	calendar.set(Calendar.MONDAY,calendar.get(Calendar.MONTH)-1);
    	calendar.set(Calendar.HOUR_OF_DAY,23);
    	calendar.set(Calendar.MINUTE,59);
    	calendar.set(Calendar.SECOND,59);
    	//calendar.roll(Calendar.DAY_OF_MONTH,-1);
    	return formatDate(getLastDayOfMonth(calendar.getTime()));
    }
    /**
     * 得到下个月第一天
     * @return
     *
     */
    public static String getFirstDayOfNextMonth(Date date){
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);
    	calendar.set(Calendar.MONDAY,calendar.get(Calendar.MONTH)+1);
    	calendar.set(Calendar.HOUR_OF_DAY,23);
    	calendar.set(Calendar.MINUTE,59);
    	calendar.set(Calendar.SECOND,59);
    	return formatDate(getFirstDayOfMonth(calendar.getTime()));
    }
    /**
     * 得到明天
     * @param date
     * @return
     * @throws ParseException 
     *
     */
    public static String getNextDay(Date date) throws ParseException{
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(parseDate(addDay(date, 1), DATE_FORMAT));
    	calendar.set(Calendar.HOUR_OF_DAY,0);
    	calendar.set(Calendar.MINUTE,0);
    	calendar.set(Calendar.SECOND,0);
    	return formatDate(calendar.getTime());
    }
    /**
     * 减天数
     * @param date
     * @param count
     * @return
     *
     */
    public static Date lowerDay(Date date,int count){
    	Calendar calendar=Calendar.getInstance();   
    	calendar.setTime(date);
    	calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)-count);
    	return calendar.getTime();
    }
    /**
     * 得到昨天
     * @param date
     * @return
     *
     */
    public static String getPreviousDay(Date date){
    	
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(lowerDay(date, 1));
    	calendar.set(Calendar.HOUR_OF_DAY,23);
    	calendar.set(Calendar.MINUTE,59);
    	calendar.set(Calendar.SECOND,59);
    	return formatDate(calendar.getTime());
    }
   /**
    * 得到上个月最后一天
    * @param date
    * @return
    *
    */
    public static Date getLastDayOfMonth(Date date){
    	
    	Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DATE,1);
		c.roll(Calendar.DAY_OF_MONTH,-1);
		c.set(Calendar.HOUR_OF_DAY,23);
		c.set(Calendar.MINUTE,59);
		c.set(Calendar.SECOND,59);
		return c.getTime();
    }
    /**
     * 得到下个月的第一天
     * @param date
     * @return
     *
     */
    public static Date getFirstDayOfMonth(Date date){
    	Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	c.set(Calendar.DATE,1);
    	c.set(Calendar.HOUR_OF_DAY,0);
		c.set(Calendar.MINUTE,0);
		c.set(Calendar.SECOND,0);
		return c.getTime();
    }
    /**
     * 返回两个日期相差的年数
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException 
     */
    public static int getDistYear(String  start,String end) throws ParseException{
    	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         Date startDate = sdf.parse(start);      
         Date endDate = sdf.parse(end);   
    	return getDistYear(startDate, endDate);
    }
    
    /**
     * 返回两个日期相差的年数
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getDistYear(Date startDate,Date endDate){
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(startDate);
    	int startYean= calendar.get(Calendar.YEAR);
    	calendar.setTime(endDate);
    	int endYean = calendar.get(Calendar.YEAR);
       	return endYean - startYean;
    }
    /**  
     * 返回两个日期相差的天数  
     * @param startDate  
     * @param endDate  
     * @return  
     * @throws ParseException  
     */  
    public static int getDistDates(Date startDate,Date endDate)     
    {   
        int totalDate = 0;   
        Calendar calendar = Calendar.getInstance();   
        calendar.setTime(startDate);   
        long timestart = calendar.getTimeInMillis();   
        calendar.setTime(endDate);   
        long timeend = calendar.getTimeInMillis();   
        totalDate = (int) (Math.abs((timeend - timestart))/(1000*60*60*24));   
        return totalDate;   
    }    
       
    /**  
     * 获取与_fromdate相差_monthCount个月的日期  
     * @param _fromdate:YYYY-MM-DD  
     * @param _monthCount  
     * @return  
     * @throws ParseException  
     */  
    public  static String getDistMonths(String _fromdate,int _monthCount) throws ParseException   
    {     
        String resultDate = "";   
        int year,month,date;   
        Calendar calendar = Calendar.getInstance();   
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(_fromdate));   
        year = calendar.get(Calendar.YEAR);   
        date = calendar.get(Calendar.DAY_OF_MONTH);   
        month = calendar.get(Calendar.MONTH) + 1 + _monthCount;   
        int c = new Integer((month-1)/12);   
        month = month%12;   
        if(month == 0)   
        month = 12;   
        year += c;   
        resultDate = year + "-" + month + "-" + date;   
        return resultDate;   
    }   
       
    /**  
     * 计算每个月的天数，以数组返回  
     * @param months  
     * @param date  
     * @return  
     * @throws ParseException  
     */  
    public static String[] countMonthDates(int months,Date date) throws ParseException     
    {     
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
        String _date = date == null ? sdf.format(new Date()) : sdf.format(date);//如果不给定起算时间则从今天算起     
  
        return countMonthDates(months,_date);     
    }      
  
    /**  
     * 计算每个月的天数，以数组返回  
     * @param months  
     * @param date  
     * @return  
     * @throws ParseException  
     */    
    public static String[] countMonthDates(int months,String date) throws ParseException     
    {     
        String[] results = null;//结果     
        String _today = date == null ? new SimpleDateFormat("yyyy-MM-dd").format(new Date()) : date;//如果不给定起算时间则从今天算起     
        int _months = months > 0 ? months : 24;//如果不给定计算的月数则计算未来两年里面的24月     
        String startDate = getDistMonths(_today,0);//获得起算日期的YYYY-MM-DD格式的字符串日期     
        results = new String[_months];   
        for(int i = 1; i <= _months; i++)     
        {     
            String nextMonthDate = getDistMonths(_today,i);//每个月的截至日期     
            String desc = startDate + " 至 " + nextMonthDate;     
            long dates = getDistDates(startDate,nextMonthDate);//返回天数   
            results[i-1] = desc + " ：共 " + dates + " 天！";   
            startDate = nextMonthDate;//当前月的结束日期作为下一个月的起始日期     
        }     
        return results;     
    }   
}  

